/*
 * @Author: wuqingchun
 * @Date: 2024-07-01 15:40:47
 * @Description: 
 * @LastEditors: wuqingchun
 * @LastEditTime: 2024-07-01 16:01:49
 */

#include "arq_allocator.h"

void* CARQAllocator::Alloc(size_t ulSize)
{
    if (m_uiBlockIndex >= MEM_BLOCK_COUNT_MAX)
    {
        return nullptr;
    }
    
    if (m_uiOffset + ulSize > MEM_BLOCK_SIZE)
    {
        // 获取新的内存块
        m_uiBlockIndex++;
    }

    if (m_arrMemBlock[m_uiBlockIndex] == nullptr)
    {
        // 获取新的内存块
        char* pcBlock = new(std::nothrow) char[MEM_BLOCK_SIZE];
        if (pcBlock == nullptr)
        {
            return nullptr;
        }

        m_arrMemBlock[m_uiBlockIndex] = pcBlock;
    }

    char* pcMem = m_arrMemBlock[m_uiBlockIndex] + m_uiOffset;
    m_uiOffset += ulSize;

    return pcMem;
}